
package stratego.java.ssm;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * <code>public final class UserInput</code>
 * <p>
 * A collection of useful and cross-class methods for dealing with situations that require user
 * input. Methods need not be cross-class, though the method should not "fit" with its class to be
 * here, if it is not cross-class.
 * </p>
 * 
 * @author Mark Aligbe
 * 
 */
public final class UserInput
{
	/** This is a utility class. As such, it should not be constructed */
	private UserInput() throws AssertionError
	{
		throw new AssertionError();
	}
	
	/**
	 * <code>public static int getBoardType(JFrame frame)</code>
	 * <p>
	 * Prompts the user for the kind of game they would like to play.
	 * </p>
	 * 
	 * @param frame
	 *            - The parent frame of this program. It's used to lock the JOptionPane to.
	 * @return The choice of the user. 0 for online play, 1 for offline play, and -1 for quitting.
	 */
	public static int getBoardType(JFrame frame)
	{
		Object[] options =
		        {"<html><span style='font-size:12px;'>2 Player Online</span></html>",
		                "<html><span style='font-size:12px;'>2 Player Offline</span></html>"};
		String message =
		        "<html><center><span style='font-size:36px;'>" + "Computer Stratego"
		                + "</span><br/>" + "<span style='font-size:16px;'>" + "By:"
		                + "</span><br/><span style='font-size:16px;'>"
		                + "Sabina Smajlaj, Seth Aaron, and Mark Aligbe" + "</span></center></html>";
		return JOptionPane.showOptionDialog(frame, message, "Welcome", JOptionPane.DEFAULT_OPTION,
		        JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
	}
	
	/**
	 * <code>public static int getDimensions(JFrame frame)</code>
	 * <p>
	 * Returns the size of the board as desired by the player. Must be an even number.
	 * </p>
	 * 
	 * @param frame
	 *            - The parent frame of this program. It's used to lock the JOptionPane to.
	 * @return An int, n, representing the dimension, and this number of grids in the board (n x n).
	 */
	public static int getDimensions(JFrame frame)
	{
		SpinnerListener l = new SpinnerListener();
		SpinnerNumberModel numberModel = new SpinnerNumberModel(10, 6, Integer.MAX_VALUE - 1, 2);
		JSpinner spinner = new JSpinner(numberModel);
		spinner.addChangeListener(l);
		final JButton button = new JButton("Go");
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				Window w = SwingUtilities.getWindowAncestor(button);
				
				if (w != null)
				{
					w.setVisible(false);
				}
			}
		});
		Object[] options = new Object[] {spinner, button};
		JOptionPane
		        .showOptionDialog(frame, "Select the board size:", "Choose Board Size",
		                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
		                options[0]);
		return l.amount;
	}
	
	/**
	 * <code>static class SpinnerListener implements ChangeListener<code>
	 * <p>Provides quick access to the amount this listener currently contains.</p>
	 * 
	 * @author Mark Aligbe
	 * 
	 */
	static class SpinnerListener implements ChangeListener
	{
		int	amount	= 10;
		
		@Override
		/**Changes the amount of this.amount to whatever value is currently shown in the spinner.*/
		public void stateChanged(ChangeEvent e)
		{
			JSpinner spinner = (JSpinner) e.getSource();
			SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
			amount = (Integer) model.getValue();
		}
	}
}
